package com.zzyl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@SpringBootTest
public class RedusTest {
    @Autowired
    private RedisTemplate<String, String> redemplate;
    @Qualifier("redisTemplate")
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void test() {
        System.out.println(redemplate);
    }

    @Test
    public void testString() {
        redisTemplate.opsForValue().set("name", "张三");
        System.out.println(redisTemplate.opsForValue().get("name"));
        // 设置带有过期时间的key
        redisTemplate.opsForValue().set("token", "123qweasd", 20, TimeUnit.SECONDS);
        System.out.println(redisTemplate.opsForValue().get("token"));

        // setnx 当指定的键key不存在时,会将key的值设置为value，返回true，否则返回false(不能覆盖原有值)
        System.out.println(redisTemplate.opsForValue().setIfAbsent("lock", "09876", 5, TimeUnit.MINUTES));
        System.out.println(redisTemplate.opsForValue().setIfAbsent("lock", "34567", 5, TimeUnit.MINUTES));
    }

    @Test
    public void testHash() {
        // hash请求   大key 小key  value
        redisTemplate.opsForHash().put("user", "name", "张三");
        redisTemplate.opsForHash().put("user", "age", "30");
        // 根据大key和小key获取值
        System.out.println(redisTemplate.opsForHash().get("user", "name"));

        // 根据大key获取所有的小key
        Set<Object> keys = redisTemplate.opsForHash().keys("user");
        System.out.println(keys);

        // 根据大key获取所有的值
        List<Object> values = redisTemplate.opsForHash().values("user");
        System.out.println(values);

        // 删除小key和值
        redisTemplate.opsForHash().delete("user", "age");
    }

    @Test
    public void testList(){
        redisTemplate.opsForSet().add("myset1","a","c","d");
        redisTemplate.opsForSet().add("myset2","b","x","y");

        Set<String> members = redisTemplate.opsForSet().members("myset1");
        System.out.println(members);

        long size = redisTemplate.opsForSet().size("myset1");
        System.out.println(size);

        Set<String> intersection = redisTemplate.opsForSet().intersect("myset1", "myset2");
        System.out.println("交集"+intersection);
        Set<String> union = redisTemplate.opsForSet().union("myset1", "myset2");
        System.out.println("并集"+union);
    }
    @Test
    public void testZset(){
        redisTemplate.opsForZSet().add("zset1","a",1);
        redisTemplate.opsForZSet().add("myzset","b",2);
        redisTemplate.opsForZSet().add("myzset3","c",3);
        Set<String> members = redisTemplate.opsForZSet().range("myzset", 0, -1);
        System.out.println(members);
        //给a承运的分数增加10
        redisTemplate.opsForZSet().incrementScore("myzset","a",10);
        //删除a,b两个成员
        redisTemplate.opsForZSet().remove("myzset","a","b");

    }
}
